package com.sqx.modules.creation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.creation.dao.FollowsUserDao;
import com.sqx.modules.creation.dao.UserBehaviorDao;
import com.sqx.modules.creation.entity.NFollowsUser;
import com.sqx.modules.creation.entity.NUserBehavior;
import com.sqx.modules.creation.entity.dto.HandleUserBehaviorDto;
import com.sqx.modules.creation.service.UserBehaviorService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户行为 服务实现类
 * </p>
 *
 * @author author
 * @since 2024年09月03日
 */
@Service
public class UserBehaviorServiceImpl extends ServiceImpl<UserBehaviorDao, NUserBehavior> implements UserBehaviorService {

    @Autowired
    private UserBehaviorDao userBehaviorDao;
    @Autowired
    private FollowsUserDao followsUserDao;

    /**
     * 关注/取消关注
     * @param userId
     * @param handleUserBehaviorDto
     * @return
     */
    @Override
    public Result handleUserBehavior(Long userId, HandleUserBehaviorDto handleUserBehaviorDto) {
        if (userId.equals(handleUserBehaviorDto.getUserId())) {
            return Result.error("不能关注自己");
        }
        LambdaQueryWrapper<NFollowsUser> followsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        followsUserLambdaQueryWrapper.eq(NFollowsUser::getUserId, userId);
        followsUserLambdaQueryWrapper.eq(NFollowsUser::getFollowsId, handleUserBehaviorDto.getUserId());
        NFollowsUser followsUser = followsUserDao.selectOne(followsUserLambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(followsUser)) {
            followsUserDao.deleteById(followsUser.getFollowsUserId());
        }else {
            NFollowsUser userFollows = new NFollowsUser();
            userFollows.setUserId(userId);
            userFollows.setFollowsId(handleUserBehaviorDto.getUserId());
            userFollows.setCreateTime(new Date());
            followsUserDao.insert(userFollows);
        }
//        if (handleUserBehaviorDto.getType() == 1) {//漫画和小说增加浏览记录
//            NUserBehavior userBehavior = new NUserBehavior();
//            userBehavior.setUserId(userId.intValue());
//            userBehavior.setUserBehaviorType(handleUserBehaviorDto.getType());
//            userBehavior.setUserBehaviorClassify(handleUserBehaviorDto.getClassify());
//            userBehavior.setChapterId(handleUserBehaviorDto.getId());
//            userBehavior.setCreateTime(new Date());
//            userBehaviorDao.insert(userBehavior);
//        } else if (handleUserBehaviorDto.getType() == 2) {//关注
//            LambdaQueryWrapper<NFollowsUser> followsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            followsUserLambdaQueryWrapper.eq(NFollowsUser::getUserId, userId);
//            followsUserLambdaQueryWrapper.eq(NFollowsUser::getFollowsId, handleUserBehaviorDto.getUserId());
//            NFollowsUser followsUser = followsUserDao.selectOne(followsUserLambdaQueryWrapper);
//            if (ObjectUtils.isNotEmpty(followsUser)) {
//                followsUserDao.deleteById(followsUser.getFollowsUserId());
//            }else {
//                NFollowsUser userFollows = new NFollowsUser();
//                userFollows.setUserId(userId);
//                userFollows.setFollowsId(handleUserBehaviorDto.getUserId());
//                userFollows.setCreateTime(new Date());
//                followsUserDao.insert(userFollows);
//            }
//        } else {
//            //社区：收藏
//            LambdaQueryWrapper<NUserBehavior> userBehaviorLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            userBehaviorLambdaQueryWrapper.eq(NUserBehavior::getUserId, userId);
//            userBehaviorLambdaQueryWrapper.eq(NUserBehavior::getUserBehaviorType, handleUserBehaviorDto.getType());
//            userBehaviorLambdaQueryWrapper.eq(NUserBehavior::getUserBehaviorClassify, 4);
//            userBehaviorLambdaQueryWrapper.eq(NUserBehavior::getChapterId, handleUserBehaviorDto.getId());
//            NUserBehavior nUserBehavior = userBehaviorDao.selectOne(userBehaviorLambdaQueryWrapper);
//            if (ObjectUtils.isNotEmpty(nUserBehavior)) {
//                userBehaviorDao.deleteById(nUserBehavior.getUserBehaviorId());
//            } else {
//                NUserBehavior userBehavior = new NUserBehavior();
//                userBehavior.setUserId(userId.intValue());
//                userBehavior.setUserBehaviorType(handleUserBehaviorDto.getType());
//                userBehavior.setUserBehaviorClassify(handleUserBehaviorDto.getClassify());
//                userBehavior.setChapterId(handleUserBehaviorDto.getId());
//                userBehavior.setCreateTime(new Date());
//                userBehaviorDao.insert(userBehavior);
//            }
//
//        }
        return Result.success();
    }
}
